package com.sfpay.coreplatform.order.kernel.boot;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
 
import com.sfpay.coreplatform.order.common.OrderResponseCode;
import com.sfpay.coreplatform.order.service.exception.OrderServiceException;
import com.sfpay.pubcenter.domain.BaseParam;
import com.sfpay.pubcenter.enums.SystemEnums.SystemName;
import com.sfpay.pubcenter.service.IConfigruationCenter;
@Component
public class CacheParam {

	@Resource
	private IConfigruationCenter configruationCenter;
	
	private static Map<String,BaseParam> paramMap = new HashMap<String,BaseParam>();

	/**
     * 
     *
     * 方法说明：初始化信息
     * 
     * @throws ServiceException
     */
	private void init() throws OrderServiceException 
	{
		if(null == paramMap)
		{
			paramMap = new HashMap<String,BaseParam>();
		}
		else if(!paramMap.isEmpty())
		{
			paramMap.clear();
		}
		try
		{
			this.loadOMSConfig();
		}
		catch(Exception e)
		{
			throw new OrderServiceException(OrderResponseCode.HESSIAN_INVOKE_EXCEPTION,
					"Load the system configuration by invoke oms system ,initialization  information failure",e);
		}
	}
	
	/**
	 * 
	 *
	 * 方法说明：加载系统配置
	 *  
	 */
	private void loadOMSConfig()
	{
		paramMap = configruationCenter.findParamBySystemName(SystemName.MEMBER);
	}
	
	 /**
     * 
     *
     * 方法说明：刷新系统信息(修改数据库配置后,刷新配置)
	 * @throws MemberServiceException 
     * 
     * @throws ServiceException
     */
	public void fresh() throws OrderServiceException
	{
		this.init();
	}
	
	/**
	 * 
	 *
	 * 方法说明：获取系统配置参数
	 *
	 * @param key
	 * @return
	 * @throws MemberServiceException 
	 */
	public BaseParam getValue(String key) throws OrderServiceException
	{
		if(null == key || "".equals(key))
		{
			return null;
		}
		else if(null == paramMap || paramMap.isEmpty())
		{
			init();
		}
		return paramMap.get(key);
	}
	
	/**
	 * 
	 *
	 * 方法说明：提供订单系统,获取缓存中配置信息
	 *
	 * @return
	 * @throws MemberServiceException 
	 */
	public Map<String,BaseParam> getCacheParam() throws OrderServiceException
	{
		if(null == paramMap)
		{
			paramMap = new HashMap<String,BaseParam>();
		}
		else if(paramMap.isEmpty())
		{
			init();
		}
		return paramMap;
	}
}
